package com.dyz.leetcode;

public class MaxProduct152 {
    public static void main(String[] args) {
        int[] nums = new int[]{2,3,-2,4};
        new MaxProduct152().maxProduct(nums);
    }
    public int maxProduct(int[] nums) {
        if(nums==null||nums.length==0){return 0;}
        //max = Max(max*num[i], num[i])
        //min = Min(min*num[i], num[i])
        int min = 1;
        int max = 1;
        int res = Integer.MIN_VALUE;
        for(int i=0; i<nums.length;i++){
            if(nums[i]<0){
                int temp = max;
                max = min;
                min = temp;
            }
            max = Math.max(max*nums[i], nums[i]);
            min = Math.min(min*nums[i], nums[i]);
            res = Math.max(res, max);
        }
        return res;
    }
}
